/* Copyright 2001,2002,2003 NAH6 BV
 * All Rights Reserved
 *
 *  $Header: /var/lib/cvs/secphone/ui/rng/fortuna/fnaPool.cpp,v 1.4 2003/11/21 16:12:58 itsme Exp $
 *
 *
 * Design of the PRNG is literally taken from 'Practical Cryptography', by Niels Ferguson, and Bruce Schneier.
 *
 */


#include "fnaPool.h"

//--------------------------------------------------------------------------
Pool::Pool()
{
    Initialize();
}
Pool::~Pool()
{
}

void Pool::Initialize()
{
    m_hash.Reset();
    m_buffer.clear();
    m_poolsize= 0;
}

void Pool::Add(const ByteVector& data)
{
    m_buffer.insert(m_buffer.end(), data.begin(), data.end());
    m_poolsize += data.size();

    if (m_buffer.size() >= Pool::HASHTHRESHOLD)
        FlushBuffer();
}
void Pool::Add(BYTE data)
{
    m_buffer.push_back(data);
    ++m_poolsize;

    if (m_buffer.size() >= Pool::HASHTHRESHOLD)
        FlushBuffer();
}

bool Pool::WaitForMutex()
{
    return m_mutex.Get();
}

void Pool::ReleaseMutex()
{
    m_mutex.Release();
}

void Pool::FlushBuffer()
{
    m_hash.Add(m_buffer);
    m_buffer.clear();
}

ByteVector Pool::Use()
{
    FlushBuffer();

    ByteVector result= m_hash.GetResult();
    m_hash.Reset();
    m_poolsize= 0;

    return result;
}

size_t Pool::GetSize() const
{
    return m_poolsize;
}


